#include "GunInfo.h"
#include "Screenshot.h"
namespace GunInfo
{
    unordered_map<unsigned int,GUN_DESC> left_guns;
    unordered_map<unsigned int,GUN_DESC> right_guns;
    std::string gun_file_name{"Guns.json"};
    std::fstream gun_file;
    nlohmann::json gun_json;

    unordered_map<unsigned int, GUN_DESC> &get_left_gun_map()
    {
        return left_guns;
    }

    unordered_map<unsigned int, GUN_DESC> &get_right_gun_map()
    {
        return right_guns;
    }

}

void GunInfo::init()
{
    try {
        if(!FS::exists(FS::path{gun_file_name})){
            std::ofstream {gun_file_name} << gun_json;
            return;
        }else{
            gun_file.open(gun_file_name);
            gun_file >> gun_json;
            gun_file.close();
            INFO(gun_json.dump(3));
            if(auto left = gun_json.find("left");left != gun_json.end()){
                for(auto item = left->begin(); item != left->end(); item++){
                    auto name = (*item)["name"].get<std::string>();
                    auto hash = (*item)["hash"].get<unsigned int>();
                    auto is_shake = (*item)["is_shake"].get<bool>();
                    left_guns[hash] = {name , is_shake};
                }
            }
            if(auto left = gun_json.find("right");left != gun_json.end()){
                for(auto item = left->begin(); item != left->end(); item++){
                    auto name = (*item)["name"].get<std::string>();
                    auto hash = (*item)["hash"].get<unsigned int>();
                    auto is_shake = (*item)["is_shake"].get<bool>();
                    right_guns[hash] = {name , is_shake};
                }
            }
            INFO(left_guns.size());
            INFO(right_guns.size());
        }
    } catch (const std::exception& e) {
        INFO(e.what());
    }
}

void GunInfo::add_left(const std::string &name, unsigned int hash, bool is_shake)
{
    nlohmann::json j;
    j["name"] = name;
    j["hash"] = hash;
    j["is_shake"] = is_shake;
    gun_json["left"].push_back(j);
    gun_file.open(gun_file_name);
    gun_file << gun_json;
    gun_file.close();
    left_guns[hash] = {name,is_shake};
}

void GunInfo::add_right(const std::string &name, unsigned int hash, bool is_shake)
{
    nlohmann::json j;
    j["name"] = name;
    j["hash"] = hash;
    j["is_shake"] = is_shake;
    gun_json["right"].push_back(j);
    gun_file.open(gun_file_name);
    gun_file << gun_json;
    gun_file.close();
    right_guns[hash] = {name,is_shake};
}

GunInfo::GUN_DESC GunInfo::current_left_gun()
{
    auto data = Screenshot::cut_frame(left_x,left_y,left_w,left_h);
    unsigned int hash{0u};
    for(auto i = 0u;i < data.size();i++){
        hash+=data.data[i];
    }
    if(left_guns.find(hash) != left_guns.end()){
        INFO("current_left_gun");
        return left_guns[hash];
    }else{
        INFO("cannot find gun");
        return left_guns.begin()->second;

    }
}

GunInfo::GUN_DESC GunInfo::current_right_gun()
{
    auto data = Screenshot::cut_frame(right_x,right_y,right_w,right_h);
    unsigned int hash{0u};
    for(auto i = 0u;i < data.size();i++){
        hash+=data.data[i];
    }
    if(right_guns.find(hash) != right_guns.end()){
        INFO("current_right_gun");
        return right_guns[hash];
    }else{
        INFO("cannot find gun");
        return right_guns.begin()->second;
    }
}
